#!/bin/bash

# date 的格式化
# https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3&apropos=0&manpath=FreeBSD+12.0-RELEASE+and+Ports

# %A	   is replaced by national representation of the full weekday name.
# 日期 周名 全名

# %a	   is replaced by national representation of the abbreviated weekday name.
# 日期 周名 缩写
echo -n "%A(周名全名) -- %a(周名缩写) : "
date "+%A -- %a"

# %B	   is replaced by national representation of the full month name.
# 日期 月名 全名

# %b	   is replaced by national representation of the abbreviated month name.
# 日期 月名 缩写
echo -n "%B(月名全名) -- %b(月名缩写) : "
date "+%B -- %b"

# %C	   is replaced by (year	/ 100) as decimal number; single digits	are preceded by a zero.
echo -n "%C 第x个百年: "
date "+%C"
# %c	   is replaced by national representation of time and date.
echo -n "%c 按国家日期和时间格式化 : "
date "+%c"

# %D	   is equivalent to ``#%m/#%d/#%y''.
echo -n "%D 以 %m/%d/%y 格式化 :"
date "+%D"
# %d	   is replaced by the day of the month as a decimal number (01-31).
echo -n "%d 带前导0的（年月）日数字 : "
date "+%d"

# %E* #%O* POSIX locale	extensions.  The sequences #%Ec #%EC #%Ex #%EX #%Ey #%EY #%Od %Oe #%OH #%OI #%Om #%OM #%OS #%Ou #%OU #%OV #%Ow #%OW #%Oy are supposed	to provide alternate representations.
#Additionally	#%OB implemented	to represent alternative months	names(used standalone, without day mentioned).
#echo -n "%EY: "
#date "+%EY"

# %e	   is replaced by the day of the month as a decimal number (1-31);single digits are preceded by a blank.
echo -n "%e 不带前导0的（年月）日数字 : "
date "+%e"

# %F	   is equivalent to ``#%Y-#%m-#%d''.
echo -n "%F 以 %Y-m-%d 格式化 : "
date "+%F"

# %G	   is replaced by a year as a decimal number with century.  This year is the one that contains the	greater	part of	the week (Monday as the first day of the week).

# %g	   is replaced by the same year	as in ``#%G'', but as a decimal number without century (00-99).
echo -n "%G(年数字包括大周) -- %g(年数字包括大周,两位) : "
date "+%G -- %g"

# %H	   is replaced by the hour (24-hour clock) as a	decimal	number (00-23).
echo -n "%H 24小时制小时数(有前导0) : "
date "+%H"

# %h	   the same as #%b.
echo -n "%h 和 %b相同 : "
date "+%h"

# %I	   is replaced by the hour (12-hour clock) as a	decimal	number (01-12).
echo -n "%I 12小时制小时数(有前导0) : "
date "+%I"

# %j	   is replaced by the day of the year as a decimal number (001-366).
echo -n "%j 一年中的第几天(1 base) : "
date "+%j"

# %k	   is replaced by the hour (24-hour clock) as a	decimal	number (0-23); single digits are preceded by a blank.
echo -n "%k 24小时制小时数(无前导0) : "
date "+%k"

# %l	   is replaced by the hour (12-hour clock) as a	decimal	number (1-12); single digits are preceded by a blank.
echo -n "%l 12小时制小时数(无前导0) : "
date "+%l"

# %M	   is replaced by the minute as	a decimal number (00-59).
echo -n "%M 分钟数(0base, 有前导0) : "
date "+%M"

# %m	   is replaced by the month as a decimal number	(01-12).
echo -n "%m 一年中的第几月(1 base，有前导0) : "
date "+%m"

# %n	   is replaced by a newline.
echo -n "%n 换行 : "
date "+%n"

# %O*   the same as #%E*.

# %p	   is replaced by national representation of either "ante meridiem" (a.m.)  or "post meridiem" (p.m.)  as appropriate.
echo -n "%p 上午输出AM，下午输出PM: "
date "+%p"

# %R	   is equivalent to ``#%H:#%M''.
echo -n "%R 以 %H:%M 格式化 : "
date "+%R"

#%r	   is equivalent to ``#%I:#%M:#%S #%p''.
echo -n "%r 以 %I:%M:%S %p 格式化 : "
date "+%r"

# %S	   is replaced by the second as	a decimal number (00-60).
echo -n "%S 秒数(0base, 有前导0) : "
date "+%S"

#%s	   is replaced by the number of	seconds	since the Epoch, UTC (see mktime(3)).
echo -n "%s 时间戳 ："
date "+%s"

# %T	   is equivalent to ``#%H:#%M:#%S''.
echo -n "%T 以 %H:%M:%S 格式化 : "
date "+%T"

# %t	   is replaced by a tab.
echo -n "%t 制表符tab : "
date "+%t"

# %U	   is replaced by the week number of the year (Sunday as the first day of the week)	as a decimal number (00-53).
echo -n "%U 一年中的第几周(0 base，有前导0，一周第一天是周日) : "
date "+%U"

# %u	   is replaced by the weekday (Monday as the first day of the week) as a decimal number (1-7).
echo -n "%u 星期几(1-7) : "
date "+%u"

# %V	   is replaced by the week number of the year (Monday as the first day of the week)	as a decimal number (01-53).  If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the	previous year, and the next week is week	1.
echo -n "%V 一年中的第几周(1 base，有前导0，一周第一天是周一, 如果1月1号所在周有4天或以上在新年，那么那个周是week 1，否则那个周是上一年最后一周) : "
date "+%V"

# %v	   is equivalent to ``#%e-#%b-#%Y''.
echo -n "%v 以 %e:%b:%Y 格式化 : "
date "+%v"

# %W	   is replaced by the week number of the year (Monday as the first day of the week)	as a decimal number (00-53).
echo -n "%W 一年中的第几周(0 base，有前导0，一周第一天是周一) : "
date "+%W"

# %w	   is replaced by the weekday (Sunday as the first day of the week) as a decimal number (0-6).
echo -n "%w 星期几(0-6，周日为0) : "
date "+%w"

# %X	   is replaced by national representation of the time.
echo -n "%X 当前国家惯用的时间格式展示 时分秒 : "
date "+%X"

#%x	   is replaced by national representation of the date.
echo -n "%x 当前国家惯用的时间格式展示 年月日 : "
date "+%x"

# %Y	   is replaced by the year with	century	as a decimal number.
echo -n "%Y 年数值: "
date "+%Y"
# %y	   is replaced by the year without century as a	decimal	number (00-99).
echo -n "%y 年数值(0base, 两位): "
date "+%y"

# %Z	   is replaced by the time zone	name.
echo -n "%Z 时区名 : "
date "+%Z"

#%z	   is replaced by the time zone	offset from UTC; a leading plus	sign stands for east of UTC, a minus sign	for west of UTC, hours and minutes follow with two digits each and no delimiter	between	them (common form	for RFC	822 date headers).
echo -n "%z 时区信息(+表示东 -表示西 两位数小时 两位数分钟) : "
date "+%z"

#%+	   is replaced by national representation of the date and time (the format is similar to	that produced by date(1)).

#%-*   GNU libc extension.	Do not do any padding when performing numeri-cal outputs.

#%_*   GNU libc extension.	Explicitly specify space for padding.

#%0*   GNU libc extension.	Explicitly specify zero	for padding.

# %%	   is replaced by `#%'.
echo -n "%% 字符% : "
date "+%%"





echo ""
echo "和年相关的"
echo -n "%C 第x个百年: "
date "+%C"
echo -n "%G(年数字包括大周) -- %g(年数字包括大周,两位) : "
date "+%G -- %g"
echo -n "%Y 年数值: "
date "+%Y"
echo -n "%y 年数值(0base, 两位): "
date "+%y"

echo ""
echo "和月相关的"
echo -n "%B(月名全名) -- %b(月名缩写) -- %h(和 %b相同): "
date "+%B -- %b -- %h"
echo -n "%m 一年中的第几月(1 base，有前导0) : "
date "+%m"

echo ""
echo "和日相关的"
echo -n "%d 日数字（带前导0） : "
date "+%d"
echo -n "%e 日数字（不带前导0） : "
date "+%e"
echo -n "%j 一年中的第几天(1 base) : "
date "+%j"
echo -n "%p 上午输出AM，下午输出PM: "
date "+%p"

echo ""
echo "和星期相关的"
echo -n "%A(周名全名) -- %a(周名缩写) : "
date "+%A -- %a"
echo -n "%u 星期几(1-7) : "
date "+%u"
echo -n "%w 星期几(0-6，周日为0) : "
date "+%w"
echo -n "%U 一年中的第几周(0 base，有前导0，一周第一天是周日) : "
date "+%U"
echo -n "%W 一年中的第几周(0 base，有前导0，一周第一天是周一) : "
date "+%W"
echo -n "%V 一年中的第几周(1 base，有前导0，一周第一天是周一, 如果1月1号所在周有4天或以上在新年，那么那个周是week 1，否则那个周是上一年最后一周) : "
date "+%V"

echo ""
echo "和小时相关的"
echo -n "%H 24小时制小时数(有前导0) : "
date "+%H"
echo -n "%k 24小时制小时数(无前导0) : "
date "+%k"
echo -n "%I 12小时制小时数(有前导0) : "
date "+%I"
echo -n "%l 12小时制小时数(无前导0) : "
date "+%l"

echo ""
echo "和分钟相关的"
echo -n "%M 分钟数(0base, 有前导0) : "
date "+%M"

echo ""
echo "和秒钟相关的"
echo -n "%S 秒数(0base, 有前导0) : "
date "+%S"
echo -n "%s 时间戳 ："
date "+%s"

echo ""
echo "时区相关的"
echo -n "%Z 时区名 : "
date "+%Z"
echo -n "%z 时区信息(+表示东 -表示西 两位数小时 两位数分钟) : "
date "+%z"

echo ""
echo "常用格式"
echo -n "%D 以 %m/%d/%y 格式化 :"
date "+%D"
echo -n "%F 以 %Y-%m-%d 格式化 : "
date "+%F"
echo -n "%v 以 %e:%b:%Y 格式化 : "
date "+%v"
echo -n "%R 以 %H:%M 格式化 : "
date "+%R"
echo -n "%r 以 %I:%M:%S %p 格式化 : "
date "+%r"
echo -n "%T 以 %H:%M:%S 格式化 : "
date "+%T"


echo "当前国家惯用格式"
echo -n "%c 按当前国家惯用的格式化展示 日期 和 时间 : "
date "+%c"
echo -n "%X 当前国家惯用的格式展示 时分秒 : "
date "+%X"
echo -n "%x 当前国家惯用的格式展示 年月日 : "
date "+%x"


echo "转义字符"
echo -n "%% 字符% : "
date "+%%"
echo -n "%t 制表符tab : "
date "+%t"
echo -n "%n 换行 : "
date "+%n"

# 日期字符串 --> 日期 --> 格式化输出
date -j -f"%Y-%m-%d" '2019-06-26' "+%y年%m月%e日%n第%j天第%V周星期%u"

# 当前日期转换时间戳
date +%s
# 特定日期转换时间戳
date -j -f"%Y-%m-%d" '2019-06-26' +%s
date -j -f"%Y-%m-%d %H:%M:%S" '2019-06-26 13:14:00' +%s

# 时间戳 转换 日期
date -j -f"%s" 1461556768 "+%Y-%m-%d %H:%M:%S"
